// pages/me/me.js
const app = getApp()
const { ajax } = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headerStyle: `background:url('https://www.hndt.com//miniIcon/AiOpera/top_bar.png') center center no-repeat; height: calc(${wx.getStorageSync('navStatusBarHeight')}px + 200rpx);`,
    resourceHeight: '',
    tabs: [],
    resourceList:[],//作品列表
    svgResourceList:[],//模型列表
    activeTab: 0,
    buttons: [{text: '确认'}],
    currentVideoUrl:'',
    currentVideoCover:'',
    dialogShow: false,
    nickName: '',
    avatarUrl: '',
    currnetIndex: 0,
    showSvg: false,
    svgFileUrl: '',
    svgData: null
  },
  // 点击播放视频
  playVideo(e){
    let videoUrl = e.currentTarget.dataset.url
    let videoCover = e.currentTarget.dataset.cover
    if(!videoUrl) return
    console.log(videoUrl)
    this.setData({
      currentVideoUrl: videoUrl,
      dialogShow: true,
      currentVideoCover: videoCover
    })
  },
  // 播放弹框
  tapDialogButton(){
    this.setData({
      dialogShow: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  goInfo(){
    wx.navigateTo({
      url: '/pages/meInfo/meInfo',
    })
  },
  onTabClick(e) {
    console.log(e)
    const index = e.detail.index
    this.setData({ 
      activeTab: index 
    })
    ajax.get('/singer/getUserCreation', {
      userId: wx.getStorageSync('userInfo').userId,
      currentPage: 1,
      pageSize: 100
    }).then(data => {
      if (data.code == 0) {
        console.log(data)
      }
    })
  },

  onChange(e) {
    console.log(e,2)
    const index = e.detail.index
    this.setData({ 
      activeTab: index 
    })
  },
  handleClick(e) {
    wx.navigateTo({
      url: './webview',
    })
  },
  handleTab(e){
    const index = e.currentTarget.dataset.index
    this.setData({ 
      currnetIndex: index 
    })
    index == 0 ? this.getResource() : this.getSvgList()
  },
  getSvgList(){
    ajax.get('/train/getUserTrain', {
      userId: wx.getStorageSync('userInfo').userId,
      // userId: '1801450717585453056',
    }).then(res => {
      if (res.code == 0) {
        this.setData({ 
          svgData: res.data 
        })
      }
    })
  },
  createSvc(){
    console.log(wx.getStorageSync('userInfo').level,'ww')
    if(wx.getStorageSync('userInfo').level == 0){
      wx.showToast({
        mask: true,
        icon: 'error',
        title: `请先开通VIP`,
      })
      return
    }
    wx.showLoading({
      mask: true,
      title: `正在加载....`,
    })
    // 检查用户文件
    ajax.get('/train/checkUserFile', {
      userId: wx.getStorageSync('userInfo').userId,
    }).then(res => {
      if (res.data) {
        console.log(res.data,'data')
        ajax.post('/train/createTrainTask', {
          userId: wx.getStorageSync('userInfo').userId,
          fileUrl: res.data.belSoundFile,
        }).then(data => {
          if (data.code == 0) {
            wx.hideLoading()
            this.setData({ 
              showSvg: false
            })
            console.log(data)
            this.getSvgList()
          }
        })
      }else{
        wx.showToast({
          mask: true,
          icon: 'none',
          title: res.msg,
        })
      }
    })
    // this.setData({ 
    //   showSvg: true 
    // })
  },
  chooseBack(){
    this.setData({ 
      showSvg: false 
    })
  },
  chooseOK(){
    // wx.showLoading({
    //   mask: true,
    //   title: `正在加载....`,
    // })
    // // 检查用户文件
    // ajax.get('/train/checkUserFile', {
    //   userId: wx.getStorageSync('userInfo').userId,
    //   fileUrl: this.data.svgFileUrl,
    //   // userId: '1801450717585453056',
    //   //     fileUrl: "https://henanopera.hndt.com/opera/2024/06/21/e5daabde-3a98-4a6b-863e-521dd1f427d4.wav",
    // }).then(res => {
    //   if (res.data) {
    //     ajax.post('/train/createTrainTask', {
    //       // userId: '1801450717585453056',
    //       // fileUrl: "https://henanopera.hndt.com/opera/2024/06/21/e5daabde-3a98-4a6b-863e-521dd1f427d4.wav",
    //       userId: wx.getStorageSync('userInfo').userId,
    //       fileUrl: this.data.svgFileUrl,
    //     }).then(data => {
    //       if (data.code == 0) {
    //         wx.hideLoading()
    //         this.setData({ 
    //           showSvg: false
    //         })
    //         console.log(data)
    //         this.getSvgList()
    //       }
    //     })
    //   }else{
    //     wx.showToast({
    //       mask: true,
    //       icon: 'none',
    //       title: res.msg,
    //     })
    //   }
    // })
  },
  handleChoose(e){
    let index = e.currentTarget.dataset.index
    // 单选
    this.data.svgResourceList.forEach((item, index) => {
      this.setData({
        [`svgResourceList[${index}].checked`]: false
      })
    })
    this.setData({
      [`svgResourceList[${index}].checked`]: true,
      svgFileUrl: this.data.svgResourceList[index].fileUrl
    })
    console.log(this.data.svgFileUrl)
  },
  getResource(){
    ajax.get('/singer/getUserCreation', {
      userId: wx.getStorageSync('userInfo').userId,
      currentPage: 1,
      pageSize: 1000
    }).then(data => {
      if (data.code == 0) {
        console.log(data)
        if(this.data.activeTab == 0){
          let resourceList = data.data.records
          resourceList.forEach(ele => {
           if(ele.roleFileImagListStr){
             let fileImg = JSON.parse(ele.roleFileImagListStr)
             ele.coverUrl = fileImg[0]
            //  svgResourceList
            let svgResourceList = this.data.svgResourceList
            svgResourceList.push({
              ...ele,
              checked: false
            })
            this.setData({
              svgResourceList: svgResourceList,
            })
           }else{
            //  默认图
            ele.coverUrl = 'https://img2.baidu.com/it/u=949199564,3603496594&fm=253&fmt=auto&app=138&f=JPEG?w=750&h=500'
           }
          })
          let resourceLine = Math.ceil(resourceList.length / 2)
          let height = 58 + 335 * resourceLine
          this.setData({
            resourceList: resourceList,
            resourceHeight: `${height}rpx`
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      nickName: wx.getStorageSync('userInfo').nickName,
      avatarUrl: wx.getStorageSync('userInfo').avg
    });
    const tabs = [
      {
        title: '作品',
        resourceList:[]
      },
      {
        title: '模型',
        resourceList:[]
      },
      // {
      //   title: '点赞',
      //   resourceList:[]
      // },
      // {
      //   title: '我的形象',
      //   resourceList:[]
      // }
    ]
    this.setData({ tabs })
    this.getResource()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})